#!usr/bin/env python
# -*- coding:utf-8 -*-

# @Author:   tian
# @project:  api.py

import requests
from db import RedisServer
from infoLog import logger

api = 'http://webapi.http.zhimacangku.com/getip?num=2&type=2&pro=340000&city=0&yys=0&port=1&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions='

def parse():
    res = requests.get(api).json()
    if res['success'] is True:
        for item in res['data']:
            ip = item['ip']
            port = item['port']
            proxy = 'http://{}:{}'.format(ip,port)
            yield proxy

def export():
    count = 0
    for item in parse():
        RedisServer().add(item,50)
        RedisServer().lpush(item)
        count += 1

    worker = len(RedisServer().getall())
    total = RedisServer().llen()
    logger.info(f'新增IP数量：{count},当前数量：{worker},历史数量：{total}')

if __name__ == '__main__':
    export()